﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Tesla.Graphics {
    /// <summary>
    /// Collection for the three main render states - Blend, DepthStencil, and Rasterizer. Setting a SamplerState to this
    /// list is meaningless and will be ignored.
    /// </summary>
    public sealed class RenderStateList : IEnumerable<RenderState> {
        private RenderState[] _renderStates;

        /// <summary>
        /// Gets the default blend state used by the engine.
        /// </summary>
        public static BlendState DefaultBlendState {
            get {
                return BlendState.Opaque;
            }
        }

        /// <summary>
        /// Gets the default depth stencil state used by the engine.
        /// </summary>
        public static DepthStencilState DefaultDepthStencilState {
            get {
                return DepthStencilState.Default;
            }
        }

        /// <summary>
        /// Gets the default rasterizer state used by the engine.
        /// </summary>
        public static RasterizerState DefaultRasterizerState {
            get {
                return RasterizerState.CullBackClockwiseFront;
            }
        }

        /// <summary>
        /// Gets the default sampler state used by the engine.
        /// </summary>
        public static SamplerState DefaultSamplerState {
            get {
                return SamplerState.LinearWrap;
            }
        }

        /// <summary>
        /// Gets the number of render states contained, which is 3.
        /// </summary>
        public int Length {
            get {
                return _renderStates.Length;
            }
        }

        /// <summary>
        /// Gets or sets the render state based on type. If type is sampler state,
        /// null will return or setting will be ignored.
        /// </summary>
        /// <param name="type">State type</param>
        /// <returns>Corresponding state, or null if not present</returns>
        public RenderState this[RenderStateType type] {
            get {
                if(type == RenderStateType.SamplerState) {
                    return null;
                }
                return _renderStates[(int)type];
            }
            set {
                if(type == RenderStateType.SamplerState) {
                    return;
                }
                _renderStates[(int)type] = value;
            }
        }

        /// <summary>
        /// Creates a new render state list.
        /// </summary>
        public RenderStateList() {
            _renderStates = new RenderState[3];
        }

        /// <summary>
        /// Clears all render states and sets their values to null.
        /// </summary>
        public void Clear() {
            Array.Clear(_renderStates, 0, _renderStates.Length);
        }

        /// <summary>
        /// Get the collection's enumerator.
        /// </summary>
        /// <returns>Enumerator</returns>
        public IEnumerator<RenderState> GetEnumerator() {
            return (IEnumerator<RenderState>) _renderStates.GetEnumerator();
        }

        IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return _renderStates.GetEnumerator();
        }

    }
}
